<template>
  <el-col :span="24" class="back">
    <affix :label="$t('business.returnBusiness')"
           :flagClass="flag === 'webchat' ? 'webchat-affix':''"
           :showTitle="showTitle"
    >
      <main>
        <el-form label-position="top"  class="approval-form">
          <b-textarea ref="bTextarea" :label="$t('business.returnReason')" require="required" :id="'backInfo'" :random="random" @blurEvent="cacheFields"></b-textarea>
          <el-button type="primary" @click.stop="submit" class="primary_btn fr" :disabled="disabledBtn">{{$t('business.cReturnBusiness')}}</el-button>
        </el-form>
      </main>
    </affix>
  </el-col>
</template>
<script>
  import BTextarea from './base/Btextarea.vue'
  import affix from '../../../ui-modules/affix/Affix.vue'
//  import {getCache} from '@/utils/m7Utils.js'
  export default {
    name: 'businessBack',
    props: {
      business: Object,
      flag: String,
      showTitle: {type: Boolean, default: true} // 是否显示title
    },
    components: {
      BTextarea,
      affix
    },
    data () {
      return {
        form: {
          _id: this.business._id
        },
        disabledBtn: false,
        random: 0
      }
    },
    methods: {
      submit (type) {
        let field = this.$refs.bTextarea || []
        let data = field.submit()
        if (data.success) {
          Object.assign(this.form, data.data)
          this.$emit('event', {event: 'backAction',
            data: {_id: this.business._id, customer: this.business.customer, form: this.form}});
        }
      },
      // 缓存事件
      cacheFields (data) {
        let busCache = this.$getCache('busCache')
        if (busCache[this.business._id + 'back']) {
          Object.assign(busCache[this.business._id + 'back'], data.data)
        }
      },
      // 重置表单
      reset () {
        if (this.$refs.bTextarea) {
          let busCache = this.$getCache('busCache')
          // 工单id存在但是缓存不存在,初始化缓存
          if (this.business._id && !busCache[this.business._id + 'back']) {
            busCache[this.business._id + 'back'] = {}
          }
          this.$refs.bTextarea.resetForm(busCache[this.business._id + 'back'] && busCache[this.business._id + 'back'].backInfo)
        }
      }
    },
    mounted () {
      // 组件挂在时重置或取缓存赋值
      this.$nextTick(() => {
        this.reset()
      })
    },
    beforeMount () {
    }
  }
</script>
<style lang="stylus" scoped>
  @import '../../../../assets/common.styl'
  .back
    padding 20px 0 0
  header
    text-align center
    padding 10px 0
    h3
      display inline-block
      color $cf-level2
  main
    .el-col
      margin-top 20px
    &:before
      content ''
      clear both
</style>
